
// pages/order/order.js
import { getMyOders } from "../../api/member"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // pageSize: 10,
    searchKeyword: '',
    list: []
  },
  async pulldownRefreshLoad({ detail }) {
    let { success, complete, page } = detail
    try {
      const status = await this.loadList(page)
      success(status)
      complete()
    } catch (err) {
      complete()
    }
  },
  loadList(page = 1) {
    let { searchKeyword } = this.data
    return getMyOders({
      page,
      keyword: searchKeyword || undefined
    }, { hasLoading: false }).then(data => {
      let list = (data.data || []).map(item => {
        let goods = item.goods[0]
        return {
          id: item.id,
          orderId: item.order_id,
          createdAt: item.created_at,
          price: item.charge,
          name: goods.goods_text,
          buyCount: 81,
          thumb: item.thumb,
          status: item.status_text,
          continuePay: item.continue_pay
        }
      })
      if (page === 1) {
        this.setData({ list: list })
      } else {
        this.setData({ list: this.data.list.concat(list) })
      }
      let status = ''
      if (list.length === 0) {
        status = page === 1 ? 'none' : 'finish'
      }
      return status
    })
  },
  onSearch(v) {
    this.setData({ searchKeyword: v.detail })
    this.selectComponent('#wPulldownRefresh').refresh()
  },

  goDetails(e) {
    wx.redirectTo({
      url: '../order-details/order-details?id=' + e.currentTarget.dataset.id
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.loadList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        active: 'order'
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})